/**
 * Copyright (c) 2008 Luciano Bustos - riaevolution.com, All Rights Reserved.
 * 
 * Contact luchyx@riaevolution.com
 * See www.riaevolution.com
 *  
 * Permission is hereby granted, free of charge, to any person obtaining a copy of 
 * this software and associated documentation files (the "Software"), to deal in 
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software.
 * 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE. 
 * */
 
package com.riaevolution.components
{
	public class DateDiscoverer
	{
		private var timeReg:RegExp = /(\d+)/i;
		private var datetypereg:RegExp = /(a(n|ñ)o(s)?)|(mes(es)?)|(semana(s)?)|(d(i|í)a(s)?)/im;
		private var reg:RegExp = /((\d+) a(n|ñ)o(s)?)|((\d+) mes(es)?)|((\d+) semana(s)?)|((\d+) d(i|í)a(s)?)/im;
		
		[Bindable]
		public var result:String;
		
		[Bindable]
		public var errorString:String;
		
		[Bindable]
		public var years:Number;
		
		[Bindable]
		public var months:Number;
		
		[Bindable]
		public var weeks:Number;
		
		[Bindable]
		public var days:Number;
		
		private var _source:String;
		private var _valid:int=0;
		
		[Bindable]
		public function set source(str:String):void {
			_source = str;
			evaluate();
		}
		public function get source():String {
			return _source;
		}
		
		
		public function isValid():Boolean {
			return _valid == 1;
		}
		
		public function evaluate():void {
			var str:String = source;
			if(!str.match(reg)) {
				_valid = 0;
				errorString = 'No se ingreso lapso de tiempo';
				//throw new Error(errorString);
				return;
			}
			errorString = '';
			var arr:Object = str.split(reg);
			var time:Number = 0;
			for(var t:String in arr) {
				var m:Array = String(arr[t]).match(reg);
				if(m) {
					switch(getType(arr[t])) {
						case 'A'://años
						time += getNumber(arr[t])*12;
						break;
						case 'M'://meses
						time += getNumber(arr[t]);
						break;
						case 'S': //semanas
						time += getNumber(arr[t])/4;
						break;
						case 'D': //dias
						time += getNumber(arr[t])/4/7;
						break;
					}
				}
			}
			months = time;
			result = "Total de "+months+" Mes"+(months>1? 'es': '');
			_valid = 1;
		}
		
		private function getNumber(str:String):Number {
			return Number(str.split(timeReg)[1]);
		}
		
		private function getType(str:String):String {
			var type:String = 'undefined';
			var matches:Array = str.match(datetypereg);
			if (matches.length<1) return type;
				if(matches[1]) {
					type = 'A';
				} else if(matches[4]) {
					type = 'M';
				} else if (matches[6]) {
					type = 'S';
				} else if(matches[8]) {
					type = 'D';
				} else {
					type = 'undefined';
				}
				return type;
			}
		}
}